package com.gzx.qc.xxljob;

import com.google.common.collect.Lists;
import com.gzx.qc.modular.voice.check.entity.GzxVoiceCheck;
import com.gzx.qc.modular.voice.check.mapper.GzxVoiceCheckMapper;
import com.gzx.qc.modular.voice.check.service.GzxVoiceCheckService;
import com.gzx.tjzy.feign.GzxCallReceiptFeign;
import com.gzx.tjzy.feign.resp.call.receipt.GzxCallReceiptDto;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.tenant.TenantContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
public class SynchronousCallDataXxlJob {

    private final GzxVoiceCheckService gzxVoiceCheckService;

    private final GzxCallReceiptFeign callReceiptFeign;

    private final GzxVoiceCheckMapper gzxVoiceCheckMapper;

//    private final SysUserService sysUserService;

    @XxlJob("SynchronousCallData")
    public void synchronousCallData() {
        log.info("开始同步通话数据------>从恒天话单表到质检表中");
        XxlJobHelper.log("开始同步通话数据------>从恒天话单表到质检表中");
        try {
            TenantContext.disableTenantFilter();
            List<GzxVoiceCheck> checkList = gzxVoiceCheckService.getVoiceCheckList();
            List<List<GzxVoiceCheck>> partition = Lists.partition(checkList, 350);
            for (List<GzxVoiceCheck> gzxVoiceChecks : partition) {
                // 根据callId查询出所有的话单信息
                List<String> callIdList = gzxVoiceChecks.stream().map(GzxVoiceCheck::getCallId).toList();
                List<GzxCallReceiptDto> callReceiptDtoList = callReceiptFeign.getCallReceiptByCallIdList(callIdList);
                Map<String, GzxCallReceiptDto> callReceiptDtoMap = callReceiptDtoList.stream()
                        .filter(callReceiptDto -> StringUtils.isNotBlank(callReceiptDto.getRingTime()))
                        .collect(Collectors.toMap(
                                GzxCallReceiptDto::getCallId,
                                callReceiptDto -> callReceiptDto,
                                (existing, replacement) -> existing,
                                HashMap::new));
                for (GzxVoiceCheck gzxVoiceCheck : gzxVoiceChecks) {
                    if (callReceiptDtoMap.containsKey(gzxVoiceCheck.getCallId())) {
                        GzxCallReceiptDto callReceiptDto = callReceiptDtoMap.get(gzxVoiceCheck.getCallId());
                        gzxVoiceCheck.setCallId(callReceiptDto.getCallId());
                        gzxVoiceCheck.setLiaisonPhone(callReceiptDto.getCallee());
                        gzxVoiceCheck.setUserName(callReceiptDto.getAgentNum());
                        gzxVoiceCheck.setFileUrl(callReceiptDto.getFileUrl());
                        gzxVoiceCheck.setAgentName(callReceiptDto.getAgentNum());
                        try {
                            if (callReceiptDto.getAgentNum() != null) {
                                String orgIdAndName = null;
                                if (orgIdAndName != null) {
                                    String[] split = orgIdAndName.split(":");
                                    gzxVoiceCheck.setDeptId(Long.parseLong(split[0]));
                                    gzxVoiceCheck.setUserName(split[1]);
                                }
                            }
                        } catch (Exception e) {
                            log.error("同步通话数据------>从恒天话单表到质检表中失败", e);
                        }
                        gzxVoiceCheck.setStatus(1);
                        try {
                            gzxVoiceCheckMapper.updateById(gzxVoiceCheck);
                        } catch (Exception e) {
                            log.error("同步通话数据更新失败", e);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("同步通话数据更新失败", e);
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }
}
